package com.wen.mall.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.wen.mall.enums.ResponseEnum;
import lombok.Data;
import org.springframework.validation.BindingResult;

/**
 * @author 20212
 */
@Data
//信息为空不返回
@JsonInclude(JsonInclude.Include.NON_NULL)

public class ResponseVo<T> {
    /**
     * 状态信息
     */
    private Integer status;

    /**
     * 消息
     */
    private String msg;
    private T data;

    private ResponseVo(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }



    private  ResponseVo(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    /**
     * 返回信息
     *
     * @param msg 自定义信息
     * @param <T>
     * @return 返回成功的信息
     */
    public static <T> ResponseVo<T> success(String msg) {
        return new ResponseVo(ResponseEnum.SUCCESS.getStatus(), msg);
    }

    /**
     * 注册成功
     *
     * @param <T>
     * @return 返回注册成功的信息
     */
    public static <T> ResponseVo<T> success() {
        return new ResponseVo(ResponseEnum.SUCCESS.getStatus(), ResponseEnum.SUCCESS.getMsg());
    }
    public static <T> ResponseVo<T> success(T data) {
        return new ResponseVo(ResponseEnum.SUCCESS.getStatus(), data);
    }
    /**
     * 发生错误
     * @param r 具体的错误信息
     * @param <T>
     * @return 返回错误信息
     */
    public static <T> ResponseVo<T> error(ResponseEnum r) {
        return new ResponseVo(r.getStatus(), r.getMsg());
    } /**
     * 发生错误
     * @param r 具体的错误代码
     * @param <T>
     * @Param msg 具体的错误信息
     * @return 返回错误信息
     */
    public static <T> ResponseVo<T> error(ResponseEnum r,String msg) {
        return new ResponseVo(r.getStatus(), msg);
    }

    /**
     * 发生错误
     * @param r 具体的错误代码
     * @param <T>
     * @param bindingResult 具体的错误信息
     * @return 返回错误信息
     */
    public static <T> ResponseVo<T> error(ResponseEnum r, BindingResult bindingResult) {
        return new ResponseVo(r.getStatus(),bindingResult.getFieldError().getField()+" "+bindingResult.getFieldError().getDefaultMessage());
    }
}
